﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//------------//
using QLKTX1._0.DTO;
using QLKTX1._0.DB;
using System.Data.SqlClient;
using System.Data;
//....................
namespace QLKTX1._0.DAO
{
    class HoaDonDienNuocDAO
    {
        public static List<HoaDonDienNuocDTO > dsHoaDonDienNuoc()
        {
            List<HoaDonDienNuocDTO> dsHoaDonDienNuoc = new List<HoaDonDienNuocDTO>();

            ConnectDB con = new ConnectDB();
            DataTable dt = con.getDataTable("SELECT * FROM hoaDon_dienNuoc");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                HoaDonDienNuocDTO dsHoaDon = new HoaDonDienNuocDTO();

                NamDTO n = new NamDTO();
                n.MaNam = Int32.Parse (dt.Rows[i]["maNam"].ToString());

                ThangDTO t = new ThangDTO();
                t.MaThang = dt.Rows[i]["maThang"].ToString();

                PhongODTO po = new PhongODTO();
                po.MaPhongO = Int32.Parse(dt.Rows[i]["maPhongO"].ToString());

                dsHoaDon.MaHoaDon = dt.Rows[i]["maHoaDon"].ToString();
                dsHoaDon.MaNam = n;
                dsHoaDon.MaThang = t;
                dsHoaDon.MaPhongO = po;
                dsHoaDon.ChiSoDienDau = Convert.ToInt32(Double.Parse(dt.Rows[i]["chiSo_dienDau"].ToString()));
                dsHoaDon.ChiSoDienCuoi = Convert.ToInt32(Double.Parse(dt.Rows[i]["chiSo_dienCuoi"].ToString()));
                dsHoaDon.ChiSoNuocDau = Convert.ToInt32(Double.Parse(dt.Rows[i]["chiSo_nuocDau"].ToString()));
                dsHoaDon.ChiSoNuocCuoi = Convert.ToInt32(Double.Parse(dt.Rows[i]["chiSo_nuocCuoi"].ToString()));
                dsHoaDon.ThanhTien = Convert.ToInt32(Double.Parse(dt.Rows[i]["thanhTien"].ToString()));
                dsHoaDon.NgayLapHoaDon = DateTime.Parse(dt.Rows[i]["ngayLapHD"].ToString());    
                 
                

                 if (dt.Rows[i]["tinhTrang_TT"].ToString().Equals("True")){
                     dsHoaDon.TinhTrangThanhToan = 1;
                 } else dsHoaDon.TinhTrangThanhToan = 0;


                dsHoaDonDienNuoc.Add(dsHoaDon);
            }
            return dsHoaDonDienNuoc;
        }
        //Ham Insert
        public static bool themHoaDon(HoaDonDienNuocDTO hd)
        {
            ConnectDB con = new ConnectDB();// dung DB

            return con.executeQuery("INSERT INTO hoaDon_dienNuoc(maNam,maThang,chiSo_dienDau,chiSo_dienCuoi,chiSo_nuocDau, chiSo_nuocCuoi,thanhTien,maPhongO,ngayLapHD,tinhTrang_TT) VALUES(" + hd.MaNam.MaNam + ",'" + hd.MaThang.MaThang + "'," + hd.ChiSoDienDau + "," + hd.ChiSoDienCuoi + "," + hd.ChiSoNuocDau  + ","+ hd.ChiSoNuocCuoi + ","+hd.ThanhTien +","+hd.MaPhongO +",'"+hd.NgayLapHoaDon +"',"+hd.TinhTrangThanhToan +")");
        }
        //Ham Delete
        public static bool xoaHoaDon(HoaDonDienNuocDTO hd)
        {
            ConnectDB con = new ConnectDB();// dung DB
            return con.executeQuery("DELETE FROM hoaDon_dienNuoc WHERE maHoaDon='" + hd.MaHoaDon + "'and maNam='" + hd.MaNam.MaNam + "' and maThang='" + hd.MaThang .MaThang  + "' and maPhongO='" + hd.MaPhongO .MaPhongO  + "'");
        }
        //Ham Update
        public static bool capNhatHoaDon(HoaDonDienNuocDTO hd)
        {
            ConnectDB con = new ConnectDB();

            return con.executeQuery("UPDATE hoaDon_dienNuoc set maNam='" + hd.MaNam.MaNam + "' and maThang='" + hd.MaThang.MaThang + "' and maPhongO='" + hd.MaPhongO.MaPhongO + "' WHERE maHoaDon='" + hd.MaHoaDon + "'");
        }

    }
}
